Ejercicio 1


In [1]:
pos = 1


Out[1]:
1

In [4]:
function brinco1()
    r = rand()
    if r < 0.5
        1
    else
        -1
    end
end


Out[4]:
brinco1 (generic function with 1 method)

In [7]:
function brinco2()
    r = 2*int(rand())- 1
end

function brinco3()
    rand(-1:2:1)
end


Out[7]:
brinco3 (generic function with 1 method)

In [13]:
brinco1()
N = 1000000
@time for i in 1:N brinco1() end


elapsed time: 0.215338835 seconds (63983688 bytes allocated, 17.61% gc time)

In [15]:
brinco2()
N = 1000000
@time for i in 1:N brinco2() end


elapsed time: 0.259386046 seconds (64073096 bytes allocated, 31.93% gc time)

In [23]:
brinco3()
N = 1000000
@time for i in 1:N brinco3() end


elapsed time: 0.3150286 seconds (63983688 bytes allocated, 23.73% gc time)

In [83]:
pos = pos + brinco1()


Out[83]:
-2

In [1]:
function primera_llegada_a_0(L::Int=100)
    pos = 1
    t = 0

    while pos > 0
        
        pos = pos + brinco1()
        
        if pos > L
            pos = L
        end
        
        t += 1
    end
    
    t
end


Out[1]:
primera_llegada_a_0 (generic function with 2 methods)

In [205]:
primera_llegada_a_0()


Out[205]:
3

In [206]:
primera_llegada_a_0(1000)


Out[206]:
3

In [207]:
primera_llegada_a_0(3.5)


`primera_llegada_a_0` has no method matching primera_llegada_a_0(::Float64)
while loading In[207], in expression starting on line 1

In [208]:
Int


Out[208]:
Int64

In [209]:
llegadas = Int[]


Out[209]:
0-element Array{Int64,1}

In [210]:
zeros(5)


Out[210]:
5-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0
 0.0

In [211]:
zeros(Int, 5)


Out[211]:
5-element Array{Int64,1}:
 0
 0
 0
 0
 0

In [2]:
function calcular_datos(L,N)
    llegadas = Int[]

    for i in 1:N
        tiempo = primera_llegada_a_0(L)
        push!(llegadas, tiempo)
    end
    
    llegadas
end


Out[2]:
calcular_datos (generic function with 1 method)

In [218]:
function calcular_datos(L, N, f::Function)
    datos = Int[]

    for i in 1:N
        resultado = f(L)
        push!(datos, resultado)
    end
    
    datos
end


Out[218]:
calcular_datos (generic function with 2 methods)

In [219]:
calcular_datos(10, 100, primera_llegada_a_0)


Out[219]:
100-element Array{Int64,1}:
  85
   3
  91
   5
   1
   3
   1
   1
   1
   1
   3
   5
   1
   ⋮
   9
 208
   3
   3
 233
   1
   1
   9
 166
  29
  65
 293

In [216]:
llegadas


Out[216]:
100-element Array{Int64,1}:
 269
  11
   1
 156
  15
   3
   1
  23
   3
  51
   1
   1
   1
   ⋮
  27
   1
   1
   1
  11
   5
   1
  19
  21
  11
   3
   1

In [241]:
datos = calcular_datos(10, 100)
mean(datos)


Out[241]:
17.01

In [222]:
100000000000. + 0.0000000001


Out[222]:
1.0e11

In [16]:
function calcular_promedios(Ls, N)
    promedios = Float64[]

    for L in Ls
        promedio = mean(calcular_datos(L, N))
        push!(promedios, promedio)
    end
    
    promedios
end


N = 10000
Ls = 10:10:300
@time promedios = calcular_promedios(Ls, N)


elapsed time: 12.344414057 seconds (8017684 bytes allocated)
Out[16]:
30-element Array{Float64,1}:
  20.1968
  39.5925
  60.1117
  82.6048
  90.7307
 115.714 
 135.944 
 172.658 
 193.482 
 195.096 
 219.452 
 214.718 
 267.872 
   ⋮     
 369.247 
 352.684 
 382.153 
 443.138 
 529.501 
 472.036 
 515.077 
 468.665 
 465.464 
 540.311 
 507.067 
 571.344 

In [8]:
using PyPlot


INFO: Loading help data...

In [14]:
Ls


Out[14]:
10:10:500

In [15]:
promedios


Out[15]:
10-element Array{Float64,1}:
  20.497 
  38.9443
  52.9675
  85.3686
  94.086 
 113.74  
 140.72  
 171.401 
 204.726 
 218.85  

In [17]:
plot(Ls, promedios, "-o")


Out[17]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x7f54e03bced0>

In [18]:
promedioα(x) = 3x


Out[18]:
promedioα (generic function with 1 method)

In [28]:
datos = calcular_datos(10, 100)
mean(datos), std(datos)


Out[28]:
(18.85,45.06972712398316)

Distribucion de prob de $T$


In [73]:
for L in [20, 50, 200]
    N = 100000
    datos = calcular_datos(L, N);
    PyPlot.hist(datos, 100, normed=true, range=(1,10000))
end
yscale("log")



In [64]:




In [58]:
?PyPlot.hist


Plot a histogram.

Compute and draw the histogram of *x*. The return value is a
tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*,
[*patches0*, *patches1*,...]) if the input contains multiple
data.

Multiple data can be provided via *x* as a list of datasets
of potentially different length ([*x0*, *x1*, ...]), or as
a 2-D ndarray in which each column is a dataset.  Note that
the ndarray form is transposed relative to the list form.

Masked arrays are not supported at present.

Parameters
----------
x : (n,) array or sequence of (n,) arrays
    Input values, this takes either a single array or a sequency of
    arrays which are not required to be of the same length

bins : integer or array_like, optional, default: 10
    If an integer is given, `bins + 1` bin edges are returned,
    consistently with :func:`numpy.histogram` for numpy version >=
    1.3.

    Unequally spaced bins are supported if `bins` is a sequence.

range : tuple, optional, default: None
    The lower and upper range of the bins. Lower and upper outliers
    are ignored. If not provided, `range` is (x.min(), x.max()). Range
    has no effect if `bins` is a sequence.

    If `bins` is a sequence or `range` is specified, autoscaling
    is based on the specified bin range instead of the
    range of x.

normed : boolean, optional, default: False
    If `True`, the first element of the return tuple will
    be the counts normalized to form a probability density, i.e.,
    ``n/(len(x)`dbin)``, ie the integral of the histogram will sum to
    1. If *stacked* is also *True*, the sum of the histograms is
    normalized to 1.

weights : array_like, shape (n, ), optional, default: None
    An array of weights, of the same shape as `x`.  Each value in `x`
    only contributes its associated weight towards the bin count
    (instead of 1).  If `normed` is True, the weights are normalized,
    so that the integral of the density over the range remains 1.

cumulative : boolean, optional, default : False
    If `True`, then a histogram is computed where each bin gives the
    counts in that bin plus all bins for smaller values. The last bin
    gives the total number of datapoints.  If `normed` is also `True`
    then the histogram is normalized such that the last bin equals 1.
    If `cumulative` evaluates to less than 0 (e.g., -1), the direction
    of accumulation is reversed.  In this case, if `normed` is also
    `True`, then the histogram is normalized such that the first bin
    equals 1.

bottom : array_like, scalar, or None, default: None
    Location of the bottom baseline of each bin.  If a scalar,
    the base line for each bin is shifted by the same amount.
    If an array, each bin is shifted independently and the length
    of bottom must match the number of bins.  If None, defaults to 0.

histtype : ['bar' | 'barstacked' | 'step' | 'stepfilled'], optional
    The type of histogram to draw.

    - 'bar' is a traditional bar-type histogram.  If multiple data
      are given the bars are aranged side by side.

    - 'barstacked' is a bar-type histogram where multiple
      data are stacked on top of each other.

    - 'step' generates a lineplot that is by default
      unfilled.

    - 'stepfilled' generates a lineplot that is by default
      filled.

align : ['left' | 'mid' | 'right'], optional, default: 'mid'
    Controls how the histogram is plotted.

        - 'left': bars are centered on the left bin edges.

        - 'mid': bars are centered between the bin edges.

        - 'right': bars are centered on the right bin edges.

orientation : ['horizontal' | 'vertical'], optional
    If 'horizontal', `~matplotlib.pyplot.barh` will be used for
    bar-type histograms and the *bottom* kwarg will be the left edges.

rwidth : scalar, optional, default: None
    The relative width of the bars as a fraction of the bin width.  If
    `None`, automatically compute the width. Ignored if `histtype` =
    'step' or 'stepfilled'.

log : boolean, optional, default : False
    If `True`, the histogram axis will be set to a log scale. If `log`
    is `True` and `x` is a 1D array, empty bins will be filtered out
    and only the non-empty (`n`, `bins`, `patches`) will be returned.

color : color or array_like of colors, optional, default: None
    Color spec or sequence of color specs, one per dataset.  Default
    (`None`) uses the standard line color sequence.

label : string, optional, default: ''
    String, or sequence of strings to match multiple datasets.  Bar
    charts yield multiple patches per dataset, but only the first gets
    the label, so that the legend command will work as expected.

stacked : boolean, optional, default : False
    If `True`, multiple data are stacked on top of each other If
    `False` multiple data are aranged side by side if histtype is
    'bar' or on top of each other if histtype is 'step'

Returns
-------
n : array or list of arrays
    The values of the histogram bins. See **normed** and **weights**
    for a description of the possible semantics. If input **x** is an
    array, then this is an array of length **nbins**. If input is a
    sequence arrays ``[data1, data2,..]``, then this is a list of
    arrays with the values of the histograms for each of the arrays
    in the same order.

bins : array
    The edges of the bins. Length nbins + 1 (nbins left edges and right
    edge of last bin).  Always a single array even when multiple data
    sets are passed in.

patches : list or list of lists
    Silent list of individual patches used to create the histogram
    or list of such list if multiple input datasets.

Other Parameters
----------------
kwargs : `~matplotlib.patches.Patch` properties

See also
--------
hist2d : 2D histograms

Notes
-----
Until numpy release 1.5, the underlying numpy histogram function was
incorrect with `normed`=`True` if bin sizes were unequal.  MPL
inherited that error.  It is now corrected within MPL when using
earlier numpy versions.

Examples
--------
.. plot:: mpl_examples/statistics/histogram_demo_features.py



Additional kwargs: hold = [True|False] overrides default hold state

In [ ]: